package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("dept:{}",dept);
        System.out.println(dept);
        deptService.add(dept);
        return Result.success();
    }

    //查询部门
    //@RequestParam 前端url路径传递的值和方法形参不一致，得加@RequestParam注解帮绑定，一致可省略
    //如果传递的参数是必须的，可以在注解中加上required的参数（默认是true）
    //前端传递的参数如果是null，可以给参数设定默认值
    @GetMapping
    public Result page(String name,Integer status,
                       @RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
        log.info("name{},status{},page{},pageSize{}",name,status,page,pageSize);
        PageResult<Dept> pageResult = deptService.page(name,status,page,pageSize);
        return Result.success(pageResult);
    }

    //根据id查部门
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("id{}",id);
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }

    //修改部门
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("dept{}",dept);
        deptService.update(dept);
        return Result.success();
    }

    //删除部门
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("id{}",id);
        deptService.delect(id);
        return Result.success();
    }

    //查询所有部门
    @GetMapping("/list")
    public Result list(){
        log.info("list");
        List<Dept> deptList = deptService.selectAllDept();
        return Result.success(deptList);
    }
}
